package com.wx.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.wx.entity.SysWxAutoReply;
import com.wx.entity.SysWxAutoReplyExample;
import com.wx.entity.WxImageReply;
import com.wx.entity.WxMulReply;
import com.wx.entity.WxPicReply;
import com.wx.entity.WxTextReply;
import com.wx.entity.WxViewReply;
import com.wx.entity.WxViewReplyExample;
import com.wx.entity.WxVoiceReply;
import com.wx.service.SysWxAutoReplyService;
import com.wx.service.WxImageReplyService;
import com.wx.service.WxMulReplyService;
import com.wx.service.WxPicReplyService;
import com.wx.service.WxTextReplyService;
import com.wx.service.WxViewReplyService;
import com.wx.service.WxVoiceReplyService;
import com.wx.util.JsonUtil;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.common.util.Page;

@Controller
@RequestMapping("admin/sub")
public class SysWxAutoReplyController extends AbsManageBaseController {

	@Resource
	private SysWxAutoReplyService sysWxAutoReplyService;
	@Resource
	private WxViewReplyService wxViewReplyService;
	@Resource
	private WxMulReplyService wxMulReplyService;
	@Resource
	private WxVoiceReplyService wxVoiceReplyService;
	@Resource
	private WxPicReplyService wxPicReplyService;
	@Resource
	private WxTextReplyService wxTextReplyService;
	@Resource
	private WxImageReplyService wxImageReplyService;

	@Value("#{propertiesReader[session_user_key]}")
	private String sessionKey;
	@Value("#{propertiesReader[session_wx_key]}")
	private String session_wx_key;

	@RequestMapping("index")
	public String index(Model model, HttpServletRequest request) {
		String wxid = getWxId(request);
		SysWxAutoReplyExample example = new SysWxAutoReplyExample();
		example.createCriteria().andWxidEqualTo(wxid);
		List<SysWxAutoReply> list = this.sysWxAutoReplyService.selectByExample(example);
		SysWxAutoReply bean = new SysWxAutoReply();
		if (list != null && list.size() > 0) {
			bean = list.get(0);
		}
		model.addAttribute("bean", bean);
		return "basic/focus/index";
	}

	@RequestMapping("persis")
	public String save(@ModelAttribute SysWxAutoReply bean, Model model, HttpServletRequest request) {
		String wxid = getWxId(request);
		bean.setWxid(wxid);
		if (null == bean.getIsMultipart()) {
			bean.setIsMultipart(0);
		}
		// 更新
		if (bean.getId() != null) {
			this.sysWxAutoReplyService.updateByPrimaryKey(bean);
		} else {
			this.sysWxAutoReplyService.insert(bean);
		}
		model.addAttribute("message", "设置成功");
		model.addAttribute("redirect", "admin/sub/index");
		return "message";
	}

	@RequestMapping("ajax")
	public void listMul(Integer currentPage, HttpServletRequest request, HttpServletResponse response) {

		String wxid = getWxId(request);

		WxViewReplyExample example = new WxViewReplyExample();
		example.createCriteria().andWxidEqualTo(wxid);
		String pageSizeStr = PropertiesUtils.getConfig("paganation_page_size");
		int pageSize = Integer.parseInt(pageSizeStr);
		if (null == currentPage) {
			currentPage = 1;
		}
		PageHelper.startPage(currentPage, pageSize);
		com.github.pagehelper.Page<WxViewReply> list = (com.github.pagehelper.Page<WxViewReply>) this.wxViewReplyService
				.selectByExample(example);
		int total = Long.valueOf(list.getTotal()).intValue();
		Page<WxViewReply> page = new Page<WxViewReply>(total, currentPage, pageSize);
		page.setRecordList(list);
		String json = JsonUtil.Object2Json(page);
		writeJson(response, json);
	}

	@RequestMapping("detail")
	public void getKeyword(Integer id, Integer type, HttpServletResponse response) {
		String keyword = "";
		if (id == null || type == null) {
			writeHtml(response, keyword);
			return;
		}
		switch (type) {
		case 5:
			WxImageReply image = this.wxImageReplyService.findOneById(id);
			if (image != null) {
				keyword = image.getKeyword();
			}
		case 4:
			WxVoiceReply voice = this.wxVoiceReplyService.selectByPrimaryKey(id);
			if (voice != null) {
				keyword = voice.getKeywords();
			}
			break;
		case 3:
			WxMulReply reply = this.wxMulReplyService.selectByPrimaryKey(id);
			if (reply != null) {
				keyword = reply.getKeyword();
			}
			break;
		case 2:
			WxPicReply pic = this.wxPicReplyService.selectByPrimaryKey(id);
			if (pic != null) {
				keyword = pic.getpKeyword();
			}
			break;
		case 1:
			WxTextReply text = this.wxTextReplyService.selectByPrimaryKey(id);
			if (text != null) {
				keyword = text.getKeyword();
			}
			break;
		default:
			break;
		}
		writeHtml(response, keyword);
	}
}
